package com.cskaoyan.service;

import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.bean.MarketTopicExample;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;

import com.cskaoyan.bean.vo.TopicVo;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.cskaoyan.util.StringUtil;
import com.cskaoyan.util.TransactionUtil;
import com.github.pagehelper.PageHelper;

import java.util.*;

/**
 * @ClassName: AdminTopicServiceImpl
 * @Description: TODO
 * @Author: jenyutao@gmail.com
 * @date: 2023/5/31 10:49
 * @Version: V1.0
 */
public class AdminTopicServiceImpl implements AdminTopicService {

    @Override
    public BaseRespVo list(int page, int limit, String sort, String order, String title, String subtitle) {


        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();
        if (title!=null&&!"".equals(title)) {
            criteria.andTitleEqualTo(title);
        }

        if (subtitle!=null&&!"".equals(subtitle)) {
            criteria.andSubtitleEqualTo(subtitle);
        }
        if (!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        criteria.andDeletedEqualTo(false);
        TransactionUtil.startTransaction();
        MarketTopicMapper mapper = TransactionUtil.getMapper(MarketTopicMapper.class);
        List<MarketTopic> marketTopicList = mapper.selectByExample(example);
        TransactionUtil.commit();

        CommonData data = CommonData.data(marketTopicList);
        BaseRespVo ok = BaseRespVo.ok(data);

        return ok;
    }

    @Override
    public BaseRespVo read(int id) {

        TransactionUtil.startTransaction();
        TopicVo topicVo = new TopicVo();
        MarketTopicMapper mapper = TransactionUtil.getMapper(MarketTopicMapper.class);
        MarketTopic marketTopic = mapper.selectByPrimaryKey(id);
        topicVo.setTopic(marketTopic);

        String[] goodsArray = marketTopic.getGoods();

        List<MarketGoods> marketGoods = new ArrayList<>();
        MarketGoodsMapper goodsMapper = TransactionUtil.getMapper(MarketGoodsMapper.class);


        for (String goods : goodsArray) {
            int i = Integer.parseInt(goods);
            marketGoods.add(goodsMapper.selectByPrimaryKey(i));
        }
        topicVo.setGoodsList(marketGoods);

        TransactionUtil.commit();
        BaseRespVo ok = BaseRespVo.ok(topicVo);
        return ok;
    }

    @Override
    public BaseRespVo create(MarketTopic marketTopic) {

        TransactionUtil.startTransaction();
        MarketTopicMapper mapper = TransactionUtil.getMapper(MarketTopicMapper.class);

        Date date = new Date();
        marketTopic.setAddTime(date);
        marketTopic.setUpdateTime(date);
        marketTopic.setId(null);
        marketTopic.setDeleted(false);

        int i = mapper.insert(marketTopic);
        TransactionUtil.commit();
        return BaseRespVo.ok(marketTopic);
    }

    @Override
    public BaseRespVo update(MarketTopic marketTopic) {

        TransactionUtil.startTransaction();
        Date date = new Date();
        marketTopic.setUpdateTime(date);
        MarketTopicMapper mapper = TransactionUtil.getMapper(MarketTopicMapper.class);
        mapper.updateByPrimaryKey(marketTopic);

        TransactionUtil.commit();

        return BaseRespVo.ok(marketTopic);

    }

    @Override
    public BaseRespVo delete(MarketTopic marketTopic) {

        TransactionUtil.startTransaction();
        marketTopic.setDeleted(true);
        MarketTopicMapper mapper = TransactionUtil.getMapper(MarketTopicMapper.class);
        mapper.updateByPrimaryKey(marketTopic);

        TransactionUtil.commit();

        return BaseRespVo.ok();
    }

    @Override
    public BaseRespVo batch(Map<String,List<Integer>> data) {

        TransactionUtil.startTransaction();


        MarketTopicMapper mapper = TransactionUtil.getMapper(MarketTopicMapper.class);
        MarketTopicExample marketTopicExample = new MarketTopicExample();

        List<Integer> list = data.get("ids");


        marketTopicExample.createCriteria().andIdIn(list);
        List<MarketTopic> marketTopics = mapper.selectByExample(marketTopicExample);
        for (MarketTopic marketTopic : marketTopics) {
            marketTopic.setDeleted(true);
            mapper.updateByPrimaryKey(marketTopic);
        }
        TransactionUtil.commit();
        return BaseRespVo.ok();
    }

    @Override
    public BaseRespVo deleteAll(Map read) {

        TransactionUtil.startTransaction();

        List ids =(List) read.get("ids");
        for (Object id : ids) {
            MarketTopicMapper mapper = TransactionUtil.getMapper(MarketTopicMapper.class);
            MarketTopic marketTopic = mapper.selectByPrimaryKey((Integer) id);
            marketTopic.setDeleted(true);

            mapper.updateByPrimaryKeySelective(marketTopic);



        }

        TransactionUtil.commit();

        return BaseRespVo.ok();
    }
}
